package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.MemberConfig;
import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberLeverSettingBinding;
import com.sgb.member.entity.MemberLevelApproveBean;
import com.sgb.member.ui.adapter.RatingBusinessAdapter;
import com.sgb.member.view_model.RatingBusinessModel;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.AppActivity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * 评级业务办理
 */
public class RatingBusinessActivity extends AppActivity  implements MemberStatusView.OnChooseListener{

    private RatingBusinessModel sModel;
    private ActivityMemberLeverSettingBinding mBinding;
    private RatingBusinessAdapter mAdapter;

    private boolean isRefresh;
    private String level = "";//会员级别
    private String memberType = "";//会员主体
    private String status ="";//调级状态
    public static void start(Context context) {
        Intent intent = new Intent(context, RatingBusinessActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_lever_setting);
        sModel = new ViewModelProvider(this).get(RatingBusinessModel.class);
        mBinding.setLifecycleOwner(this);
        mBinding.activityBar.titleView.setText("评级业务办理");
        mBinding.activityBar.ivBack.setOnClickListener(v -> finish());
        registerObserver();
        initRecyclerView();
        initPopup();
        isRefresh = true;
        loadData(false);
        sModel.getMemberLevelData(); // 会员级别
        sModel.getMemberStateData(); // 会员状态
        sModel.getMemberZhuTi(); // 会员主体
    }
    
    private void loadData(boolean isLoadMore){
        sModel.getMixMemberLevelApproveListByPage(isLoadMore,level,status,memberType);
    }

    private void initPopup() {
        mBinding.popupMemberLever.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popupState.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popupZhuTi.initPopup(mBinding.grayLayout, mBinding.popLine, this);
    }

    @Override
    public void onLoadChooseData(String mKeyName) {

    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        switch (mKeyName) {
            case "调整等级":
                    level = entity.getId();
                    loadData(false);
                break;
            case "调级状态":
                if (!TextUtils.equals(no, status)) {
                    status = entity.getNo();
                    loadData(false);
                }
                break;
            case "会员主体":
                if (!TextUtils.equals(no, memberType)) {
                    memberType = entity.getNo();
                    loadData(false);
                }
                break;
        }
    }

    private void initRecyclerView() {
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                isRefresh = false;
                loadData(true);
            }
        });
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new RatingBusinessAdapter(this, null);
        mBinding.recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<MemberLevelApproveBean>() {
            @Override
            public void onItemClick(MemberLevelApproveBean memberLevelApproveBean, int position) {
                sModel.getH5Url(memberLevelApproveBean, new SimpleResult<String>() {
                    @Override
                    public void onResult(String s, Object... objects) {
                        MemberConfig.getInstance().startWebViewX5(getActivity(), s);
                    }
                });
            }
        });
    }

    private void registerObserver() {
        sModel.dialog.observe(this, aBoolean -> {
            if (aBoolean) {
                if (!DialogHelper.isShowing()) {
                    DialogHelper.showProgressDialog(this, "数据获取中...");
                }
            } else {
                DialogHelper.dismissProgressDialog();
                mBinding.recyclerView.refreshComplete();
            }
        });
        sModel.empty.observe(this, aBoolean -> {
            if (aBoolean) {
                mAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            } else {
                mBinding.emptyView.setVisibility(View.GONE);
            }
        });
        sModel.hasLoadMore.observe(this, aBoolean ->
                mBinding.recyclerView.setNoMore(!aBoolean));
        sModel.mLeverData.observe(this, memberLeverBeans -> {
            if (!isRefresh) {
                if (memberLeverBeans != null && memberLeverBeans.size() > 0) {
                    mAdapter.addDatas(memberLeverBeans);
                }
            } else {
                if (memberLeverBeans != null && memberLeverBeans.size() > 0) {
                    mBinding.emptyView.setVisibility(View.GONE);
                    mAdapter.setDatas(memberLeverBeans);
                } else {
                    mAdapter.clearDatas();
                    mBinding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        });
        sModel.memberLeveLiveData.observe(this, commonPopEntities ->
                mBinding.popupMemberLever.setData(commonPopEntities, level));
        sModel.memberStateLiveData.observe(this, commonPopEntities ->
                mBinding.popupState.setData(commonPopEntities,status));
        sModel.memberZhuTiLiveData.observe(this, commonPopEntities ->
                mBinding.popupZhuTi.setData(commonPopEntities, memberType));
    }
}
